Thread: Help Understanding Logic of these two programs to print the value of e and e^x

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    12

    Lightbulb Help Understanding Logic of this program to print the value of e

    I get the factorial part but what is why have we used n . What is accuracy, degree, and what is meant by while( n <= accuracy )
    Write a program to compute the value of e
    e=1+1/1!+1/2!+1/3!+⋯



    Code:
    #include<stdio.h>
    int main()
        
    {
      int n = 0;                 /* loop counter for accuracy */
      int fact = 1;              /* current n factorial */
      int accuracy = 10;         /*degree of accuracy */
      double e = 0;              /* current estimated value of e */
                                 /* loop until degree of accuracy */
          while (n <= accuracy)
        {
        if (n == 0)
           {
          fact *= 1;
          }
       
        else {
          fact *= n;
        }
        e += 1.0 / fact;
        ++n;
        }
      printf("e is %f\n", e);   /* display estimated value */
      return 0;
    }
    Last edited by Salem; 11-15-2015 at 05:46 AM. Reason: fix train wreck tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > What is accuracy, degree, and what is meant by while( n <= accuracy )
    Why don't you edit the program to say

    int accuracy = 0;
    compile and run

    Then
    int accuracy = 1;
    compile and run


    Or better yet,
    for ( accuracy = 0 ; accuracy <= 10 ; accuracy++ )

    It's called software for a reason.
    You can change it however you want. Make a prediction, make a change, observe the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What does line 14 actually achieve?

    Edit: I should say that I actually like that you compute the factorial as a "running total" (but my question still stands)
    Last edited by Hodor; 11-15-2015 at 06:10 AM.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    0! = 1

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Izzy98 View Post
    0! = 1
    Yes, of course... but it's already 1 (because you initialised it to 1)

    Edit: In my test program I changed it to:

    Code:
    if (n > 1) {
        fact *= n;
    }
    Last edited by Hodor; 11-15-2015 at 07:05 AM.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I wonder if there is a way to improve accuracy/precision by rearranging the equation?

    On my computer:
    Code:
    for n == 18 e is identical to the previous value of e
    for n == 19 e is identical to the previous value of e
    for n == 20 e is identical to the previous value of e
    for n == 21 e is identical to the previous value of e
    This is due to hitting the limit of double's precision.

    For what it's worth I think the name of the variable "accuracy" is misleading. It's not really a measure of accuracy, it's the number of terms to sum in an infinite sum (for this method of estimating e which I think Newton first described). Because the particular continuing fraction is asymptotic the greater the number of terms in the sum the closer the calculated value of e is to the real value of e. Edit: this isn't really a measure of the "accuracy" though.

    Edit 2: "degree" is sometimes used in mathematics and often synonymous with "order" which is simply the number of times something happens, or the size of something. (In this case the number of iterations). I don't think I'd use the word degree or order for this particular problem though.
    Last edited by Hodor; 11-15-2015 at 07:26 AM.

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    That makes more sense.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Here is my test program if you want to examine it. It's not very good and depending on your CPU/OS factorial 21 might be too big for unsigned long long.

    Code:
    #include <stdio.h>
    
    double estimate_e(unsigned terms);
    
    int main(void)
    {
        const int accuracy = 21;   /* "degree" of "accuracy" */
        double e;
    
        e = estimate_e(accuracy);
        printf("e is %.17f\n", e);
    
        return 0;
    }
    
    double estimate_e(unsigned terms)
    {
        unsigned i = 0;
        unsigned long long fact = 1;    /* current i factorial */
        double e = 0;                   /* current estimated value of e */
    
        while (i <= terms) {
            double prev_e = e;
            if (i > 1) {
                fact *= i;
                /* should check for overflow of 'fact' here */
            }
            e += 1.0 / fact;
            if (e == prev_e) /* it's bad to use == with floating point operands, but here I chose to use it */
                printf("for n == %u e is identical to the previous value of e\n", i);
            i++;
        }
    
        return e;
    }
    On my computer this gives e accurate to 14 decimal places.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in understanding the logic
    By baxy in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2013, 06:41 AM
  2. Understanding some Logic
    By skg29 in forum C Programming
    Replies: 7
    Last Post: 10-09-2011, 07:44 AM
  3. Help me out with following programs logic in c ??
    By shaan_046 in forum C Programming
    Replies: 1
    Last Post: 09-26-2011, 02:18 PM
  4. i cant understanding this logic
    By nkrao123@gmail. in forum C Programming
    Replies: 6
    Last Post: 10-26-2009, 01:51 AM
  5. Understanding the pseudocode/logic for this program
    By Sholcomb1 in forum C Programming
    Replies: 11
    Last Post: 12-08-2006, 02:07 PM

Tags for this Thread